<? require_once("sitemap.php"); ?>
<?
$action="start";

if (!empty($_POST['action']))
{
	$action = $_POST['action'];
}
?>
<html>
	<title><? echo (":" . $HTTP_SERVER_VARS['REMOTE_USER'] . ": ". $Botname); ?>'s EL-BOT guild Trade Management</title>
<body>
<?
	if ($action=="add")
	{
		$id=intval(mysql_real_escape_string($_POST['id']));
		$name=($_POST['name']);
		for($i=0;$i<strlen($name);$i++)
		{
			if($name[$i]=='\\')
			{
				$i++;
			}
			$t = $t . $name[$i];
		}
		$name = $t;
		$nameX=0;
		$i=strlen($name);
		if($i>0 && $i<5)
		{
			$j=$i-1;
			while($j>-1)
			{
				$nameX*=256;
				$nameX+=ord($name[$j]);
				$j--;
			}
			$rank=(mysql_real_escape_string($_POST['rank']));
			if($rank=="Banned") $rank=-1;
			if($rank=="Normal") $rank=0;
			if($rank=="Prefer") $rank=1;
		
			$notes=(mysql_real_escape_string($_POST['notes']));
			//echo "guild name [$name] name2 [$nameX] ";

			$sql="SELECT id FROM guilds WHERE lower(guildname)='" . strtolower($nameX) . "' AND botid='$botid'";
			$result = mysql_query($sql,$db);
			if(mysql_num_rows($result)==0)
			{
				$sql = "INSERT INTO guilds (guildname,rank,botid,notes) VALUES ('$nameX','$rank','$botid','$notes')";
				$result = mysql_query($sql,$db);
			}
			else
			{
				$action="update";
			}
		}
		else
			echo "guildlist not updated. guildless players can not be given better prices";
	
	}
	if ($action=="update")
	{
		dumppost($HTTP_SERVER_VARS['REMOTE_USER']);						
		$id=intval(mysql_real_escape_string($_POST['id']));
		$name=($_POST['name']);
		for($i=0;$i<strlen($name);$i++)
		{
			if($name[$i]=='\\')
			{
				$i++;
			}
			$t = $t . $name[$i];
		}
		$name = $t;
		$nameX=0;
		$i=strlen($name);
		if($i>0 && $i<5)
		{
			$j=$i-1;
			while($j>-1)
			{
				$nameX*=256;
				$nameX+=ord($name[$j]);
				//echo "j $j nameX $nameX ";
				$j--;
				
			}
			$rank=(mysql_real_escape_string($_POST['rank']));
			if($rank=="Banned") $rank=-1;
			if($rank=="Normal") $rank=0;
			if($rank=="Prefer") $rank=1;
		
			$notes=(mysql_real_escape_string($_POST['notes']));
			//echo "guild name [$name] name2 [$nameX] ";

			$sql = "UPDATE guilds SET rank='$rank',guildname='$nameX',notes='$notes' WHERE id='$id' AND botid='$botid';";
			//echo $sql;
			$result = mysql_query($sql,$db);
			//echo (mysql_error());
			//echo "<BR>" . mysql_affected_rows() . " affected rows";
		}
		else
			echo "guildlist not updated. guildless players can not be given better prices";
	}
	if ($action=="delete")
	{
		dumppost($HTTP_SERVER_VARS['REMOTE_USER']);						
		$id=intval(mysql_real_escape_string($_POST['id']));
		$sql = "DELETE FROM guilds WHERE id='$id' AND botid='$botid' LIMIT 1;";
		$result = mysql_query($sql,$db);		
		//echo "<BR>" . mysql_affected_rows() . " affected rows";
	}
	//echo (mysql_error());
?>
	<h1><? echo ($Botname); ?>'s EL-BOT guild Trade Management</h1>
	<h2>Add new member</h2>
	<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
	<input type="hidden" name="action" value="add">
	<table border=0 cellpadding="0" cellspacing="0">
	<tr><td>name</td><td><input type="text" name="name" value=""></td><td><i>guild name<br>CasE sensitive</i></td></tr>
	<tr><td>rank</td><td><input type="radio" name="rank" value="Banned">Banned<br>
	<tr><td>rank</td><td><input type="radio" name="rank" value="Normal" checked>Normal<br>
	<tr><td>rank</td><td><input type="radio" name="rank" value="Prefer">Prefer</td>
	<td><i>guild rank (Banned means ban entire guild, Normal means no special privileges, Prefer gives member rates)</i></td></tr>
	<tr><td>notes</td><td><input type="text" name="notes"></td><td><i>Note here why you changed this guild</i></td></tr>
	<td><input type="submit" name="submit" value="ADD"></td><td><i>Click here to save this data</i></td></tr></table>
	</form><br>

	<h2>guild list:</h2>
	<table border="1" cellpadding="0" cellspacing="0">
		<tr>
			<td align='center'>id</td>
			<td align='center'>guildname</td>
			<td align='center'>rank</td>
			<td align='center'>notes</td>
			<td align='center'>Function</td>
		</tr>
		<?php
			$sql = "SELECT id, guildname, rank, notes FROM guilds WHERE botid='" .$botid."' ORDER BY rank DESC, guildname ASC";
			$result = mysql_query($sql,$db);

			while ($myrow = mysql_fetch_row($result))
			{
		?>
			<tr>
			<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
				<input type="hidden" name="id" value="<?echo ($myrow[0])?>">
				<input type="hidden" name="action" value="update">
				<td><?echo ($myrow[0])?></td>
				<?
				$nameY="";
				$name=intval($myrow[1]);
				$t1=$name & 255;
				$name=$name>>8;
				$t2=$name & 255;
				$name=$name>>8;
				$t3=$name & 255;
				$name=$name>>8;
				$t4=$name & 255;

				if($t1!=0)
					$nameY .= chr($t1);

				if($t2!=0)
					$nameY .= chr($t2);
				if($t3!=0)
					$nameY .= chr($t3);
				if($t4!=0)
					$nameY .= chr($t4);
				?>
				<td><input type="text" name="name" value="<?echo ($nameY)?>"></td>
				<td>
					<input type="radio" name="rank" value="Banned"<? if($myrow[2]<0) echo " checked"?>>Banned<br>
					<input type="radio" name="rank" value="Normal"<? if($myrow[2]==0) echo " checked"?>>Normal<br>
					<input type="radio" name="rank" value="Prefer"<? if($myrow[2]>0) echo " checked"?>>Prefer<br>
				</td>
				<td><input type="text" name="notes" value="<?echo ($myrow[3])?>"></td>
				<td><input type="submit" name="submit" value="UPDATE">
			</form>
			<form method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
				<input type="hidden" name="id" value="<?echo ($myrow[0])?>">
				<input type="hidden" name="action" value="delete">
				<input type="submit" name="submit" value="DELETE"></td>
			</form>
			</tr>
			<?
			}
			?>
	</table>
</body>
</html>
